﻿using AutoMapper;
using MediatR;
using Traceability.Doamin;
using Traceability.ErrorCode;
using Traceability.Infratruvture.Interfares;
using Traceability.Write.Api.Application.Command;

namespace Traceability.Write.Api.Application.CommandHander
{
    public class AddImmunityHander : IRequestHandler<AddImmunityCommand, ApiResult<int>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<Immunity> baseRequsitory;

        public AddImmunityHander(IMapper mapper, IBaseRequsitory<Immunity> baseRequsitory)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
        }

        /// <summary>
        /// 添加免疫信息操作
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(AddImmunityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var entity = mapper.Map<Immunity>(request);

            var list = await baseRequsitory.AddAsync(entity);

            if (list > 0)
            {
                result.Code = ApiEnum.添加成功;
                result.Msg = ApiEnum.添加成功.ToString();
                result.Data = list;
            }
            else
            {
                result.Code = ApiEnum.添加失败;
                result.Msg = ApiEnum.添加失败.ToString();
                result.Data = list;
            }

            return await Task.FromResult(result);
        }
    }
}
